<template>
	<div class="system-user-dialog-container">
		<el-dialog
			:close-on-click-modal="false"
			:title="dataForm.userId ? $t('common.editBtn') : $t('common.addBtn')"
			draggable
			v-model="visible"
			v-if="visible"
			width="600"
			@close="onClose"
		>
			<el-form :model="dataForm" :rules="dataRules" label-width="90px" ref="dataFormRef" v-loading="loading">
				<el-row :gutter="20">
					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.processCode')" prop="process_code">
							<el-input clearable v-model="dataForm.process_code" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.materialCode')" prop="material_code">
							<el-input clearable v-model="dataForm.material_code" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.materialName')" prop="material_name">
							<el-input clearable v-model="dataForm.material_name" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.spec')" prop="specification">
							<el-input clearable v-model="dataForm.specification" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.unit')" prop="unit">
							<el-input clearable v-model="dataForm.unit" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" class="mb-6">
						<el-form-item :label="$t('pickApply.applyQty')" prop="apply_qty">
							<el-input clearable v-model="dataForm.apply_qty"  type="number"></el-input>
						</el-form-item>
					</el-col>

				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onClose" icon="close">{{ $t('common.cancelButtonText') }}</el-button>
					<el-button @click="onSubmit" type="primary" icon="check" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" name="spDialog" setup>
import { useMessage } from '/@/hooks/message';
import { UpdateDetailApplyQty } from '/@/api/platform/wms/pickApply';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();
// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);

// 通知父组件刷新
const emit = defineEmits(['refresh']);

const dataForm = reactive({

});
const dataRules = ref({
});
// 打开弹窗
const openDialog = async (row) => {
	visible.value = true;
	console.log(row)
	dataForm.process_code=row.process_code;
	dataForm.material_code=row.material_code;
	dataForm.material_name=row.material_name;
	dataForm.specification=row.specification;
	dataForm.unit=row.unit;
	dataForm.apply_qty=row.apply_qty;
	dataForm.pickapplyDetailId=row.id;
	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
};

const onClose = () => {
	visible.value = false;
	dataForm.materialCode = '';
	dataFormRef.value?.resetFields();
};


// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		await UpdateDetailApplyQty({
			pickapplyDetailId: dataForm.pickapplyDetailId,
			applyQty: dataForm.apply_qty,
		}).then((res) => {
			if (!res.ok) {
				useMessage().error(res.data);
			}
		});
		visible.value = false; // 关闭弹窗
		emit('refresh');
	} catch (error: any) {
		useMessage().error(error.msg);
	} finally {
		loading.value = false;
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
